package drds.configuration.ddlparser.util;

import drds.configuration.ddlparser.ddl.CreateTable;
import drds.configuration.ddlparser.ddl.SqlObject;
import drds.configuration.ddlparser.ddl.add.AddColumn;
import drds.configuration.ddlparser.ddl.update.UpdateColumn;
import drds.configuration.ddlparser.parser.Lexer;
import drds.configuration.ddlparser.parser.Parser;
import drds.configuration.ddlparser.sql.CreateAlterColumn;
import drds.configuration.ddlparser.sql.CreateCreateTable;
import drds.configuration.ddlparser.sql.CreateUpdateColumn;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 我愿是一只美丽的蝴蝶,悄悄的改变整个世界
 */
public class DDLPreDeal {
    /**
     * 控制台提交的DDL语句需要进行预处理
     */
    public static void main(String[] args) throws Exception {
        String basePath = DDLPreDeal.class.getClassLoader().getResource("").getPath().replace("/target/classes/", "\\src\\main\\java\\");
        basePath = basePath + DDLPreDeal.class.getPackage().getName().toString().replace(".", File.separator);
        String path = basePath + File.separator + "test.txt";
        System.out.println(path);
        List<String> list = parseDDLList(path);
        for (String ddl : list) {
            System.out.println("ddl语句:" + ddl);
            Lexer lexer = new Lexer(ddl);
            Parser parser = new Parser(lexer);
            SqlObject sqlObject = parser.parseDDL();

            if (sqlObject instanceof CreateTable) {
                System.out.println("生成语句:");

                System.out.print((CreateCreateTable.sql((CreateTable) sqlObject)));
                System.out.println();
            } else if (sqlObject instanceof AddColumn) {
                System.out.println("生成语句:");

                System.out.print(CreateAlterColumn.sql((AddColumn) sqlObject));
                System.out.println();
            } else if (sqlObject instanceof UpdateColumn) {
                System.out.println("生成语句:");

                System.out.print(CreateUpdateColumn.sql((UpdateColumn) sqlObject));
                System.out.println();
            }
            //break;
        }
    }

    /**
     * 解析DDL list,前端提交的是\r\n
     */
    public static List<String> parseDDLList(String path) throws FileNotFoundException, IOException {
        File file = new File(path);
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        List<String> list = new ArrayList<String>();
        //
        String line = null;
        StringBuffer sb = new StringBuffer("");
        /**
         * 没有以;进行结尾
         */
        boolean isEndWith$ = false;
        while ((line = bufferedReader.readLine()) != null) {
            if (line.contains("###")) {
                continue;
            } else if ("".equals(line.trim())) {
                continue;
            } else if (line.endsWith(";")) {
                sb.append(line.trim());
                list.add(sb.toString());
                isEndWith$ = true;//new object
                sb = new StringBuffer("");
            } else {
                sb.append(line.trim());
                isEndWith$ = false;
            }

        }
        if (!isEndWith$) {
            throw new IllegalArgumentException("ddl脚本不是以;为结尾的");
        }
        return list;
    }
}
